package viewmodel;

import java.util.List;

import org.zkoss.bind.BindUtils;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.util.media.Media;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Image;

import util.FileUtils;

import admin.dao.AdsDAO;



public class AdsDataVM {
	
	private List<AdsEditStatus> adsEditData;
	private boolean displayEdit = true;
	
	public AdsDataVM(){
		adsEditData = new AdsDataModel().convertAdsToEditData();
	}
	
	public List<AdsEditStatus> getAdsEditData() {
		return adsEditData;
	}

	public boolean isDisplayEdit() {
		return displayEdit;
	}

	@Command
	public void changeEditableStatus(@BindingParam("adsEditStatus")AdsEditStatus aes){
		aes.setEditingStatus(!aes.isEditingStatus());
		refreshRowTemplate(aes);
	}
	
	@Command
	public void confirm(@BindingParam("adsEditStatus")AdsEditStatus aes){
		changeEditableStatus(aes);
		AdsDAO.updateAds(aes);
		refreshRowTemplate(aes);
	}
	@Command
	public void upload(@BindingParam("adsEditStatus") AdsEditStatus aes,@BindingParam("media")Media media){
		try{
				String filePath = FileUtils.saveFileFromMedia(media, null, "adversite");
				aes.getAds().setImage(filePath);
				AdsDAO.updateAds(aes);
		}catch (Exception e) {
			// TODO: handle exception
		}
		refreshRowTemplate(aes);
	}
	public void refreshRowTemplate(AdsEditStatus adsEditData){
		BindUtils.postNotifyChange(null, null, adsEditData, "editingStatus");
	}
}
